﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess ;
using MShop.Core.Domain;
using MShop.Core.Extensions;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class AreaGroupController: SecureController
    {
        private readonly IAreaGroupDao  _AreaDao;

        public AreaGroupController(IAreaGroupDao areaDao)
        {           
            _AreaDao = areaDao;           
        }
        
        public ActionResult Index(int? id)
        {
            ViewData["list"] = _AreaDao.Find(x => x.ParentId == null);
            if (id.HasValue)
            {
                return View(_AreaDao.GetById(id));
            }
            return View(new AreaGroup());
        }


        //public ActionResult Create()
        //{
        //    ViewData["selectlist"] = _AreaDao.BuildSelectList(null,true);
        //    return View(new AreaGroup());
        //}

        [HttpPost]
        public ActionResult Create(AreaGroup area,int? areaid)
        {

       
            if (ModelState.IsValid)
            {
                area.ParentId = areaid;
                _AreaDao.Save(area);
                _AreaDao.SaveChanges();

                return RedirectToAction(Request.Params["cname"], new { areaid = areaid });
            } 
          
            return View(area);
        }

        public ActionResult Edit(int id)
        {
            var area = _AreaDao.GetById(id);
            ViewData["selectlist"] = _AreaDao.BuildSelectList(area.ParentId, true);
            return View(area);
        }


        public ActionResult Delete(int id,int? areaid)
        {
            var area = _AreaDao.GetById(id);
            _AreaDao.Delete(area);
            _AreaDao.SaveChanges();
            string actionname = Request.Params["an"];
            return RedirectToAction(actionname, new { areaid = areaid });
        }


        [HttpPost]
        public ActionResult Edit(int id, AreaGroup area,int? areaid)
        {
            if (!string.IsNullOrEmpty(area.Name))
            {
                var dbarea = _AreaDao.GetById(id);
                UpdateModel<AreaGroup>(dbarea, new string[] { "ParentId", "Name", "Zip" });
                _AreaDao.SaveChanges();
            }
             return RedirectToAction(Request.Params["cname"], new { areaid = areaid });            
        }



        public ActionResult PIndex(int areaid,int? id)
        {
            ViewData["list"] = _AreaDao.Find(x => x.ParentId == areaid);
            ViewData["PModel"] = _AreaDao.GetById(areaid);
            if (id.HasValue)
            {
                return View(_AreaDao.GetById(id.Value));
            }
            return View(new AreaGroup());
        }

        public ActionResult CIndex(int areaid, int? id)
        {
            ViewData["list"] = _AreaDao.Find(x => x.ParentId == areaid);
            ViewData["PModel"] = _AreaDao.GetById(areaid);
            if (id.HasValue)
            {
                return View(_AreaDao.GetById(id.Value));
            }
            return View(new AreaGroup());
        }

        public ActionResult DIndex(int areaid, int? id)
        {
            ViewData["list"] = _AreaDao.Find(x => x.ParentId == areaid);
            ViewData["PModel"] = _AreaDao.GetById(areaid);
            if (id.HasValue)
            {
                return View(_AreaDao.GetById(id.Value));
            }
            return View(new AreaGroup());
        }

        public ActionResult GetAreas(int areaid)
        {
            var query = from item in  _AreaDao.GetAll() where item.ParentId==areaid select new{item.Name,item.Id};

            return Json(query, JsonRequestBehavior.DenyGet);
        }
    }
}
